package www.yingdi365.com.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.yanzhenjie.recyclerview.swipe.touch.OnItemMoveListener;
import com.yanzhenjie.recyclerview.swipe.touch.OnItemMovementListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import www.yingdi365.com.R;
import www.yingdi365.com.base.ZjbBaseActivity;
import www.yingdi365.com.constant.Constant;
import www.yingdi365.com.model.AppInvitation;
import www.yingdi365.com.model.AppMagazine;
import www.yingdi365.com.model.QingJian;
import www.yingdi365.com.model.QingJianBean;
import www.yingdi365.com.model.ZaZhi;
import www.yingdi365.com.util.GsonUtils;
import www.yingdi365.com.viewholder.YeMainTZViewHolder;

public class YeMainTZActivity extends ZjbBaseActivity implements View.OnClickListener {

    private TextView textViewRight;
    private SwipeMenuRecyclerView swipeMenuRecyclerView;
    public RecyclerArrayAdapter<QingJianBean> adapter;
    private AppInvitation appInvitation;
    private AppMagazine appMagazine;
    public List<QingJianBean> qingJianBeanList = new ArrayList<>();
    private boolean isBianJiQJ = false;
    private boolean isBianJiZZ = false;
    private ZaZhi zaZhi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ye_main_tz);
        init();
    }

    @Override
    protected void initSP() {

    }

    @Override
    protected void initIntent() {
        Intent intent = getIntent();
        appInvitation = (AppInvitation) intent.getSerializableExtra(Constant.INTENT_KEY.appInvitation);
        appMagazine = (AppMagazine) intent.getSerializableExtra(Constant.INTENT_KEY.appMagazine);
        if (appInvitation!=null){
            isBianJiQJ = true;
            QingJian qingJian = GsonUtils.parseJSON(appInvitation.getRenderData(), QingJian.class);
            qingJianBeanList.add(qingJian.getFirst());
            qingJianBeanList.addAll(qingJian.getMiddles());
            qingJianBeanList.add(qingJian.getLast());
        }else {
            isBianJiQJ = false;
        }
        if (appMagazine!=null){
            isBianJiZZ=true;
            zaZhi = GsonUtils.parseJSON(appMagazine.getRenderData(), ZaZhi.class);
            qingJianBeanList.addAll(zaZhi.getPages());
        }else {
            isBianJiZZ=false;
        }

    }

    @Override
    protected void findID() {
        textViewRight = (TextView) findViewById(R.id.textViewRight);
        swipeMenuRecyclerView = (SwipeMenuRecyclerView) findViewById(R.id.recycler_view);
    }

    @Override
    protected void initViews() {
        ((TextView) findViewById(R.id.textViewTitle)).setText("页面调序删除");
        textViewRight.setText("完成");
        swipeMenuRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        swipeMenuRecyclerView.setAdapter(adapter = new RecyclerArrayAdapter<QingJianBean>(YeMainTZActivity.this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new YeMainTZViewHolder(parent, R.layout.item_yemian_tz, viewType);
            }

            @Override
            public int getViewType(int position) {
                if (position == 0) {
                    return 1;
                } else if (position == getCount() - 1) {
                    return 2;
                } else {
                    return 0;
                }
            }
        });
        swipeMenuRecyclerView.setLongPressDragEnabled(true);
        swipeMenuRecyclerView.setOnItemMoveListener(onItemMoveListener);// 监听拖拽，更新UI。
        swipeMenuRecyclerView.setOnItemMovementListener(onItemMovementListener);
    }

    @Override
    protected void setListeners() {
        findViewById(R.id.imageBack).setOnClickListener(this);
        textViewRight.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        adapter.clear();
        adapter.addAll(qingJianBeanList);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.textViewRight:
                if (isBianJiQJ){
                    List<QingJianBean> middleList = new ArrayList<>();
                    for (int i = 1; i < qingJianBeanList.size() - 1; i++) {
                        middleList.add(qingJianBeanList.get(i));
                    }
                    QingJian qingJian = new QingJian(920, qingJianBeanList.get(qingJianBeanList.size() - 1), 640, qingJianBeanList.get(0), middleList);
                    Intent intent= new Intent();
                    intent.setAction(Constant.BROADCASTCODE.XiuGaiQJSX);
                    intent.putExtra(Constant.INTENT_KEY.QingJian,GsonUtils.parseObject(qingJian));
                    sendBroadcast(intent);
                    finish();
                }
                if (isBianJiZZ){
                    zaZhi.setPages(qingJianBeanList);
                    Intent intent= new Intent();
                    intent.setAction(Constant.BROADCASTCODE.XiuGaiQJSX);
                    intent.putExtra(Constant.INTENT_KEY.QingJian,GsonUtils.parseObject(zaZhi));
                    sendBroadcast(intent);
                    finish();
                }
                break;
            case R.id.imageBack:
                finish();
                break;
        }
    }

    /**
     * 当Item被移动之前。
     */
    public OnItemMovementListener onItemMovementListener = new OnItemMovementListener() {
        /**
         * 当Item在移动之前，获取拖拽的方向。
         * @param recyclerView     {@link RecyclerView}.
         * @param targetViewHolder target ViewHolder.
         * @return
         */
        @Override
        public int onDragFlags(RecyclerView recyclerView, RecyclerView.ViewHolder targetViewHolder) {
            // 我们让第一个不能拖拽。
            if (targetViewHolder.getAdapterPosition() == 0 || targetViewHolder.getAdapterPosition() == (qingJianBeanList.size() - 1)) {
                return OnItemMovementListener.INVALID;// 返回无效的方向。
            } else {
                return OnItemMovementListener.DOWN | OnItemMovementListener.UP | OnItemMovementListener.LEFT | OnItemMovementListener.RIGHT;
            }

        }

        @Override
        public int onSwipeFlags(RecyclerView recyclerView, RecyclerView.ViewHolder targetViewHolder) {
            return OnItemMovementListener.INVALID;// 其它均返回无效的方向。
        }
    };

    /**
     * 当Item移动的时候。
     */
    private OnItemMoveListener onItemMoveListener = new OnItemMoveListener() {
        @Override
        public boolean onItemMove(int fromPosition, int toPosition) {
            if (toPosition == 0 || toPosition == (qingJianBeanList.size() - 1)) {// 保证第一个不被挤走。
                return false;
            }
            if (fromPosition < toPosition)
                for (int i = fromPosition; i < toPosition; i++)
                    Collections.swap(qingJianBeanList, i, i + 1);
            else
                for (int i = fromPosition; i > toPosition; i--)
                    Collections.swap(qingJianBeanList, i, i - 1);
            adapter.notifyItemMoved(fromPosition, toPosition);
            return true;// 返回true表示处理了，返回false表示你没有处理。
        }

        @Override
        public void onItemDismiss(int position) {
            // 当Item被滑动删除掉的时候，在这个Activity是无效的，因为这里没有启用这个功能。
            // 使用Menu时就不用使用这个侧滑删除啦，两个是冲突的。
        }
    };
}
